<template>
  <el-card class="m-b-20">
    <div class="d-flex">
    <el-link :underline="false" class="m-r-5" :href="generateUrl(user.userId)">
      <el-button type="primary" plan icon="el-icon-document">生成简历</el-button>
    </el-link>
    <el-upload
      v-if="user.userId"
      class="upload-demo inline-block m-r-5"
      :headers="headers"
      with-credentials
      :limit="1"
      :action="uploadUrl"
      :show-file-list="false"
      :data="uploadFileData()"
      :on-success="uploadFileSuccess"
    >
      <el-button type="success" icon="el-icon-upload2">更新简历</el-button>
    </el-upload>
    <el-link v-if="user.userId && resumeId" class="m-r-5" :underline="false">
      <el-button type="info" plan icon="el-icon-tickets" @click="previewResume">预览简历</el-button>
    </el-link>
    <el-link v-if="user.userId && resumeId" :underline="false" :href="downloadUrl(resumeId)">
      <el-button type="primary" plan icon="el-icon-printer">下载简历</el-button>
    </el-link>
    </div>
  </el-card>
</template>

<script>
import { getAuthentication, getAuthenticationHeader } from '@/utils/store'

export default {
  name: 'UserResume',
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          userId: undefined
        }
      }
    }
  },
  data() {
    return {
      resumeId: undefined,
      uploadUrl: this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/user/resume/update`,
      headers: getAuthenticationHeader()
    }
  },
  computed: {
    generateUrl() {
      return function(value) {
        return encodeURI(this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/user/resume/generate/${value}?access_token=` + getAuthentication())
      }
    },
    printUrl() {
      return function(value) {
        return encodeURIComponent(this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/user/resume/print/${value}?filename=${value}_resume.pdf&access_token=` + getAuthentication())
      }
    },
    downloadUrl() {
      return function(value) {
        return encodeURI(this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/api/file/download/${value}?access_token=` + getAuthentication())
      }
    },
    uploadFileData() {
      return function() {
        if (this.resumeId) {
          return {
            file_id: this.resumeId,
            path: 'resume',
            user_id: this.user.userId
          }
        }
        return {
          path: 'resume',
          user_id: this.user.userId
        }
      }
    }
  },
  mounted() {
    this.$nextTick(this.loadForm)
  },
  methods: {
    loadForm() {
      this.$http.get(`${process.env.VUE_APP_MODULE_ADMIN_API}/api/system/user/resume/` + this.user.userId).then(response => {
        this.resumeId = response.param
      }).catch(e => {})
    },
    previewResume() {
      let url = this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/office/preview?isOffice=true`
      url += '&access_token=' + this.$store.state.user.token
      url += '&url=' + this.printUrl(this.user.userId)
      // 若是文件，则进行相应的预览
      window.open(url, '查看文件', '_blank')
    },
    uploadFileSuccess(response) {
      if (response.code === 200) {
        this.resumeId = response.param
        this.$message.success('上传成功')
      } else {
        this.$message.error(response.msg + ': ' + response.param)
      }
    }
  }
}
</script>
